﻿using cn.jpush.api;
using cn.jpush.api.common;
using cn.jpush.api.common.resp;
using cn.jpush.api.push.mode;
using cn.jpush.api.push.notification;
using RecruitStudents.Utility.Const;
using RecruitStudents.Utility.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RecruitStudents.Utility.Push
{
    public class PushHelper
    {
        /// <summary>
        /// 推送
        /// </summary>
        /// <param name="title">咨询互动的标题对象</param>
        /// <param name="reply">咨询互动的回复对象</param>
        public void Push(TB_ConsultTitle title, TB_ConsultReply reply)
        {

            if (title == null || reply == null)
            {
                SetLog("*****TB_ConsultTitle或者TB_ConsultReply为Null，无法推送******!" + "\n");
                return;
            }
            if (String.IsNullOrEmpty(title.TypeID))
            {
                SetLog("*****TB_ConsultTitle的类型TypeID为空，无法推送******!" + "\n");
                return;
            }

            switch (title.TypeID)
            {
                case ConsultType.GG:
                    PushGongGao(title, reply);
                    break;
                case ConsultType.TZ:
                    PushTongZhi(title, reply);
                    break;
                case ConsultType.ZX:
                    PushZiXun(title, reply);
                    break;

            }
        }
        private void PushZiXun(TB_ConsultTitle title, TB_ConsultReply reply)
        {

            if (title == null || reply == null)
            {
                SetLog("*****TB_ConsultTitle或者TB_ConsultReply为Null，无法推送******!" + "\n");
                return;
            }

            SetLog("*****开始推送咨询******" + "\n");
            SetLog("*****开始发送******" + "\n");

            JPushClient client = new JPushClient(app_key, master_secret);
            PushPayload payload = PushObject_android_and_ios_alias(title.UsersID, title.ID, reply.ID, title.ConsultTitle, title.TypeID);
            try
            {
                var result = client.SendPush(payload);
                SetLog("ResponseResult: " + result.ResponseResult);
                //由于统计数据并非非是即时的,所以等待一小段时间再执行下面的获取结果方法
                System.Threading.Thread.Sleep(10000);
                /*如需查询上次推送结果执行下面的代码*/
                var apiResult = client.getReceivedApi(result.msg_id.ToString());
                SetLog("apiResult: " + apiResult.getErrorMessage());

            }
            catch (APIRequestException e)
            {
                SetLog("Error response from JPush server. Should review and fix it. ");
                SetLog("HTTP Status: " + e.Status);
                SetLog("Error Code: " + e.ErrorCode);
                SetLog("Error Message: " + e.ErrorCode);
            }
            catch (APIConnectionException e)
            {
                SetLog(e.Message);
            }
            SetLog("*****结束发送******");

        }

        private void PushGongGao(TB_ConsultTitle title, TB_ConsultReply reply)
        {

            if (title == null || reply == null)
            {
                SetLog("*****TB_ConsultTitle或者TB_ConsultReply为Null，无法推送******!" + "\n");
                return;
            }

            SetLog("*****开始推送公告******" + "\n");
            SetLog("*****开始发送******" + "\n");

            JPushClient client = new JPushClient(app_key, master_secret);
            PushPayload payload = PushObject_android_and_ios_all(title.ID, reply.ID, title.ConsultTitle, title.TypeID);
            try
            {
                var result = client.SendPush(payload);
                SetLog("ResponseResult: " + result.ResponseResult);
                //由于统计数据并非非是即时的,所以等待一小段时间再执行下面的获取结果方法
                System.Threading.Thread.Sleep(10000);
                /*如需查询上次推送结果执行下面的代码*/
                var apiResult = client.getReceivedApi(result.msg_id.ToString());
                SetLog("apiResult: " + apiResult.getErrorMessage());

            }
            catch (APIRequestException e)
            {
                SetLog("Error response from JPush server. Should review and fix it. ");
                SetLog("HTTP Status: " + e.Status);
                SetLog("Error Code: " + e.ErrorCode);
                SetLog("Error Message: " + e.ErrorCode);
            }
            catch (APIConnectionException e)
            {
                SetLog(e.Message);
            }
            SetLog("*****结束发送******");

        }

        private void PushTongZhi(TB_ConsultTitle title, TB_ConsultReply reply)
        {

            if (title == null || reply == null)
            {
                SetLog("*****TB_ConsultTitle或者TB_ConsultReply为Null，无法推送******!" + "\n");
                return;
            }

            SetLog("*****开始推送通知******" + "\n");
            SetLog("*****开始发送******" + "\n");

            JPushClient client = new JPushClient(app_key, master_secret);
            PushPayload payload = PushObject_android_and_ios_alias(title.UsersID, title.ID, reply.ID, title.ConsultTitle, title.TypeID);
            try
            {
                var result = client.SendPush(payload);
                SetLog("ResponseResult: " + result.ResponseResult);
                //由于统计数据并非非是即时的,所以等待一小段时间再执行下面的获取结果方法
                System.Threading.Thread.Sleep(10000);
                /*如需查询上次推送结果执行下面的代码*/
                var apiResult = client.getReceivedApi(result.msg_id.ToString());
                SetLog("apiResult: " + apiResult.getErrorMessage());

            }
            catch (APIRequestException e)
            {
                SetLog("Error response from JPush server. Should review and fix it. ");
                SetLog("HTTP Status: " + e.Status);
                SetLog("Error Code: " + e.ErrorCode);
                SetLog("Error Message: " + e.ErrorCode);
            }
            catch (APIConnectionException e)
            {
                SetLog(e.Message);
            }
            SetLog("*****结束发送******");

        }



        private void SetLog(String log)
        {

        }


        private const String app_key = "98314abc857fb4a1ee4d0dac";
        private const String master_secret = "39d4f679ad644c8f9cf90dca";
        private const String title = "您有新的消息,请注意查收！";

        private static PushPayload PushObject_android_and_ios_all(String tid, String rid, String message, String type)
        {
            PushPayload pushPayload = new PushPayload();
            pushPayload.platform = Platform.all();
            pushPayload.audience = Audience.all();
            var notification = new Notification();
            notification.setAlert(message);
            //android
            notification.AndroidNotification = new AndroidNotification();
            notification.AndroidNotification.setTitle(title);
            notification.AndroidNotification.setBuilderID(1);
            notification.AndroidNotification.AddExtra("ti", tid);
            notification.AndroidNotification.AddExtra("ri", rid);
            notification.AndroidNotification.AddExtra("t", type);
            //IOS
            notification.IosNotification = new IosNotification();
            notification.IosNotification.setSound(title);
            notification.IosNotification.setBadge(1);
            notification.IosNotification.setContentAvailable(true);
            notification.IosNotification.AddExtra("ti", tid);
            notification.IosNotification.AddExtra("ri", rid);
            notification.IosNotification.AddExtra("t", type);

            pushPayload.notification = notification.Check();
            return pushPayload;
        }

        private static PushPayload PushObject_android_and_ios_alias(String alias, String tid, String rid, String message, String type)
        {
            return PushObject_android_and_ios_alias(new String[] { alias }, tid, rid, message, type);
        }

        private static PushPayload PushObject_android_and_ios_alias(String[] alias, String tid, String rid, String message, String type)
        {
            PushPayload pushPayload = new PushPayload();
            pushPayload.platform = Platform.android_ios();
            var audience = Audience.s_alias(alias);
            pushPayload.audience = audience;
            var notification = new Notification();
            notification.setAlert(message);
            //android
            notification.AndroidNotification = new AndroidNotification();
            notification.AndroidNotification.setTitle(title);
            notification.AndroidNotification.setBuilderID(1);
            notification.AndroidNotification.AddExtra("ti", tid);
            notification.AndroidNotification.AddExtra("ri", rid);
            notification.AndroidNotification.AddExtra("t", type);
            //IOS
            notification.IosNotification = new IosNotification();
            notification.IosNotification.setSound(title);
            notification.IosNotification.setBadge(1);
            notification.IosNotification.setContentAvailable(true);
            notification.IosNotification.AddExtra("ti", tid);
            notification.IosNotification.AddExtra("ri", rid);
            notification.IosNotification.AddExtra("t", type);

            pushPayload.notification = notification.Check();


            return pushPayload;
        }

    }
}
